#include "stdafx.h"
#include "EmailArg.h"

#include <fstream>
#include <stdexcept>
#include <vector>
#include <string>

using std::ifstream;
using std::ofstream;
using std::endl;
using std::string;
using std::vector;

EmailArg::EmailArg()
{

}

void EmailArg::getArgs(std::vector<std::string> &args) const
{
	args.resize(ArgCount);
	args[EmailAddress] = m_EmailAddress;
	args[Username] = m_Username;
	args[Password] = m_Password;
	args[PopServer] = m_PopServer;
	args[SmtpServer] = m_SmtpServer;
}

void EmailArg::setArgs(const std::vector<std::string>& args)
{
	if (args.size() < ArgCount)
	{
		throw std::invalid_argument("The args vector not enough");
	}
	m_EmailAddress = args[EmailAddress] ;
	m_Username = args[Username];
	m_Password = args[Password];
	m_PopServer = args[PopServer];
	m_SmtpServer =args[SmtpServer] ;
}

void EmailArg::getArgsFromFile(const std::string& fileName)
{
	ifstream argFileStream(fileName.c_str());
	if(!argFileStream)
	{
		throw std::invalid_argument("Can't open the arguments file to read");
	}
	argFileStream >> m_EmailAddress;
	argFileStream >> m_Username;
	argFileStream >> m_Password;
	argFileStream >> m_PopServer;
	argFileStream >> m_SmtpServer;
}

void EmailArg::saveArgsToFile(const std::string &fileName) const
{
	ofstream argFileStream(fileName.c_str(), ofstream::trunc);
	if (!argFileStream)
	{
		throw std::invalid_argument("Can't open the arguments file to write");
	}

	argFileStream << m_EmailAddress << endl;
	argFileStream << m_Username << endl;
	argFileStream << m_Password << endl;
	argFileStream << m_PopServer << endl;
	argFileStream << m_SmtpServer << endl;
}